package cn.ht.countDownLatchAndCyclicBarrierDemo;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

/**
 * @author ht
 * @Description:
 * @date 2020-5-21
 */
public class CyclicBarrierDemo {

    public static int num = 7;

    public static void main(String[] args) {

        CyclicBarrier cyclicBarrier = new CyclicBarrier(num, () -> {
            System.out.println("======>集齐龙珠，召唤神龙");
        });
        for (int i = 1; i <= 7 ; i++) {
            final int finalI = i;
            new Thread(()->{
                System.out.println(Thread.currentThread().getName()+"\t收集到了"+ finalI +"星珠");
                try {
                    cyclicBarrier.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (BrokenBarrierException e) {
                    e.printStackTrace();
                }
            },peopleEnum.getPeople(i).getMessage()).start();
        }
    }

}
